package com.fc.advbox.utils;

import java.util.Comparator;

/**
 * the wrapped comparator is used when we have a comparator used for an object and we want to apply
 * it to some structure that contains that object that needs to be unwrapped.
 * 
 * @param <Main> the main object we are sorting
 * @param <Sub> the sub object that is extracted from the main
 */
public abstract class WrappedComparator<Main, Sub> implements Comparator<Main> {
    
    private Comparator<Sub> _subCompare;
    
    /**
     * takes in the comparator used to compare the sub objects. 
     * 
     * @param subCompare
     */
    public WrappedComparator( Comparator<Sub> subCompare ) {
        _subCompare = subCompare;
    }
    
    /**
     * method which takes in a main object and unwraps it for us.
     * 
     * @param object
     * @return the sub object
     */
    public abstract Sub unwrap( Main object );

    /**
     * @see Comparator#compare(Object, Object)
     */
    @Override
    public final int compare(Main first, Main second) {
        return _subCompare.compare( unwrap(first), unwrap(second) );
    }

}
